package com.xcdh.trading.service;

import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Service
public class QQEmailService {

    private final static String email = "1534375215@qq.com" ;
    private final static String password = "kfrrveqropkkiahb" ;
    private final static Properties props = new Properties();

    private final static  Session session = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(email, password);
        }
    });
    static {
        props.put("mail.smtp.host", "smtp.qq.com");  // 根据你的邮箱类型修改SMTP服务器地址
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    }

    public void sendQQEmail(String subject , String text){
        try {
            // 创建邮件对象
            Message message = new MimeMessage(session);
            // 设置发件人
            message.setFrom(new InternetAddress(email));
            // 设置收件人，与发件人相同
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
            // 设置邮件主题
            message.setSubject(subject);
            // 设置邮件内容
            message.setText(text);
            // 发送邮件
            Transport.send(message);
            System.out.println("邮件发送成功！");
        } catch (MessagingException e) {
            System.out.println("邮件发送失败：" + e.getMessage());
        }
    }

}
